package com.yololabelmaker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CMainWindow extends JFrame {
    // 根据BDD图定义parts和properties
    private CImgArea m_imgArea; // parts: CImgArea[0..1]
    private JButton m_saveLabelsBtn; // properties: m_saveLabelsBtn
    
    // 菜单栏相关组件
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenuItem openFileItem;
    private JMenuItem openDirectoryItem;
    private JMenuItem saveLabelsItem;
    private JMenuItem exitItem;
    
    public CMainWindow() {
        initUI();
    }
    
    private void initUI() {
        // 设置窗口默认关闭操作，确保窗口关闭时应用程序退出
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // 创建菜单栏
        menuBar = new JMenuBar();
        fileMenu = new JMenu("文件");
        
        openFileItem = new JMenuItem("打开图片");
        openFileItem.addActionListener(e -> openImage());
        
        openDirectoryItem = new JMenuItem("打开数据集目录");
        openDirectoryItem.addActionListener(e -> openDatasetDirectory());
        
        saveLabelsItem = new JMenuItem("保存标签");
        saveLabelsItem.addActionListener(e -> saveLabels());
        
        exitItem = new JMenuItem("退出");
        exitItem.addActionListener(e -> System.exit(0));
        
        fileMenu.add(openFileItem);
        fileMenu.add(openDirectoryItem);
        fileMenu.addSeparator();
        fileMenu.add(saveLabelsItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
        
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        
        // 创建图片区域
        m_imgArea = new CImgArea();
        JScrollPane scrollPane = new JScrollPane(m_imgArea);
        mainPanel.add(scrollPane, BorderLayout.CENTER);
        
        // 创建底部控制面板
        JPanel controlPanel = new JPanel(new BorderLayout());
        
        // 创建缩放控制面板
        JPanel zoomPanel = new JPanel();
        JLabel zoomLabel = new JLabel("缩放: ");
        JTextField zoomTextField = new JTextField(5);
        zoomTextField.setText("100%");
        
        JButton zoomInBtn = new JButton("放大");
        zoomInBtn.addActionListener(e -> {
            double currentScale = m_imgArea.getScaleFactor();
            m_imgArea.setScaleFactor(currentScale * 1.2);
            zoomTextField.setText(String.format("%.0f%%", m_imgArea.getScaleFactor() * 100));
        });
        
        JButton zoomOutBtn = new JButton("缩小");
        zoomOutBtn.addActionListener(e -> {
            double currentScale = m_imgArea.getScaleFactor();
            m_imgArea.setScaleFactor(currentScale * 0.8);
            zoomTextField.setText(String.format("%.0f%%", m_imgArea.getScaleFactor() * 100));
        });
        
        JButton resetZoomBtn = new JButton("重置");
        resetZoomBtn.addActionListener(e -> {
            m_imgArea.setScaleFactor(1.0);
            zoomTextField.setText("100%");
        });
        
        JCheckBox fitToWindowCheckBox = new JCheckBox("适应窗口");
        fitToWindowCheckBox.setSelected(true);
        fitToWindowCheckBox.addActionListener(e -> {
            m_imgArea.setFitToWindow(fitToWindowCheckBox.isSelected());
            if (fitToWindowCheckBox.isSelected()) {
                zoomTextField.setText(String.format("%.0f%%", m_imgArea.getScaleFactor() * 100));
            }
        });
        
        zoomPanel.add(zoomLabel);
        zoomPanel.add(zoomTextField);
        zoomPanel.add(zoomInBtn);
        zoomPanel.add(zoomOutBtn);
        zoomPanel.add(resetZoomBtn);
        zoomPanel.add(fitToWindowCheckBox);
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        m_saveLabelsBtn = new JButton("保存标签");
        m_saveLabelsBtn.addActionListener(e -> saveLabels());
        
        JButton clearLabelsBtn = new JButton("清除标签");
        clearLabelsBtn.addActionListener(e -> m_imgArea.clearLabels());
        
        buttonPanel.add(m_saveLabelsBtn);
        buttonPanel.add(clearLabelsBtn);
        
        // 将缩放面板和按钮面板添加到底部控制面板
        controlPanel.add(zoomPanel, BorderLayout.NORTH);
        controlPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        mainPanel.add(controlPanel, BorderLayout.SOUTH);
        
        setContentPane(mainPanel);
        
        // 设置窗口初始尺寸和位置
        setSize(800, 600); // 设置为800x600的小尺寸窗口
        setLocationRelativeTo(null); // 居中显示
    }
    
    private void openImage() {
        // 创建图片打开器并打开图片
        CImgOpener imgOpener = new CImgOpener();
        imgOpener.openImage(m_imgArea);
    }
    
    private void openDatasetDirectory() {
        // 创建图片打开器并打开数据集目录
        CImgOpener imgOpener = new CImgOpener();
        imgOpener.openDatasetDirectory(m_imgArea);
    }
    
    private void saveLabels() {
        // 保存标签逻辑
        if (m_imgArea != null && m_imgArea.hasImage()) {
            m_imgArea.saveLabels();
        } else {
            JOptionPane.showMessageDialog(this, "请先打开一张图片");
        }
    }
    
    // 主方法，程序入口
    public static void main(String[] args) {
        // 在事件调度线程中创建和显示GUI
        SwingUtilities.invokeLater(() -> {
            CMainWindow window = new CMainWindow();
            window.setVisible(true);
        });
    }
}